/* This object is here to bring whatever you want out into the global space to look at.
	There are probably more elegant ways to do it, but meh.
*/
debug = {};

/*
	Creates an alert element with the message and specified type.
	
	@messge - The message you'd like to display in the alert
	@type - error, info, success
	returns - HTML element
*/

/*
	Function displayAlert
	Displays information to the user to be acted upon.
*/

function displayAlert(messageToAppend){
	
	var message = $(document.createElement('li')).append(messageToAppend);
	$("#alertInfo").append(message).fadeIn();	
}


/*
	Function escapeData
		Removes escape characters from data so that they display properly
*/
function escapeData(inputData){
	return inputData.replace(/\\./g, '$1');
}


/*
	Function showLoadingIndicator
		Displays the "loading" indicator. Useful for indicating to the user that something is happening.
*/
function showLoadingIndicator(){
	$("div.popover#loading").fadeIn();
}

/*
	Function hideLoadingIndicator
		Displays the "loading" indicator. Useful for indicating to the user that something is happening.
*/
function hideLoadingIndicator(){
	$("div.popover#loading").fadeOut();
}


/*
	Function showSection
		Displays the related section on the page when a tab has been clicked. The click binding is done in the index document
		
		object @eventOb - The event object generated by the click. Passed by the anonymous function in the click bind.
*/
/*
//DEPRECATED
function showSection(eventOb){
	$(".section").hide();
	$("#"+$(eventOb.target).closest("a.btn").attr("data-section")).show();
}
*/

function showSection(eventOb){
	$(".section").hide();
	var navElement = $("#"+eventOb.target.id);
	$("ul.nav li.active").removeClass("active");
	
	$(navElement).closest("li").addClass("active");
	$("#"+$(eventOb.target).closest("a").attr("data-section")).show();
}

/*
	Method: displayWishlist
	
	Builds and displays current User's wishlist. Requrires JSON userlist object.
	If the user listed in object is the current user, we can display edit buttons.
	
	@displayData
		boolean @isCurrentUser - the toggle to determine which toolset to display
		String @toolset - the name of the toolset to include (edit, shop)
		Object @list - the javascript object item list: contains information to build rows.
		String @targetTable - where to put this list. 
*/
function displayWishlist(displayData){
	//debug = displayData;
	//The table we're plugging this into.
	table = $("#"+displayData.targetTable);
	
	table.attr("data-listOwner",displayData.forUserId);
		
	/*
	Builds the Table header and puts the columns into a definable order.
	*/
	if(displayData.skipHeader == true){
		jQuery("#"+displayData.targetTable+" tr.itemRow").remove();
		
	}else{
		table.html("");
		hRow = $(document.createElement("tr")).addClass("hidden-phone");
		
		$(displayData.columns).each(function(i,e){
				var th = $(document.createElement("th")).append(e.columnName)
				var sortIcon = $(document.createElement("i")).addClass("sortIcon icon pull-right").attr("id","sort"+e.columnName)
				th.append(sortIcon);
				
				if(e.columnHeaderStyles != undefined){
					th.addClass(e.columnHeaderStyles);
				}
				
				if(e.sortFunctions[0] != undefined){
					th.addClass("sortable");
					th.toggle(
						function() {
							$("#"+$(this).closest("table")[0].id+" i.sortIcon").removeClass("icon-chevron-up icon-chevron-down");
							if(displayData.list != undefined && displayData.list.length > 1){
								e.sortFunctions[0](displayData);
							}
							$(this).children("i").addClass("icon-chevron-up");
						},
						function(){
							$("#"+$(this).closest("table")[0].id+" i.sortIcon").removeClass("icon-chevron-up icon-chevron-down");
							if(displayData.list != undefined && displayData.list.length > 1){
								e.sortFunctions[1](displayData);
							}
							$(this).children("i").addClass("icon-chevron-down");
						}
					)
				}
				hRow.append(th);
				
		});
		
		table.append(hRow);		
	}

	//Loop through each item on the user list and add it to a row, which is then added to the table.
	
	if(displayData.list != null){
		$(displayData.list).each(function(i,e){
			row = $(document.createElement("tr"))
				.attr("data-itemid",e.itemid)
				.attr("id","item_"+e.itemid+"_row")
				.attr("data-toggle","collapse")
				.attr("data-target","detail"+i)
				.addClass("itemRow");
		
			//Generates any needed display versions of properties. Builds display-specific items depending on toolset value.
			e.toolset = displayData.toolset;
			e = generateDisplayElements(e);
		
			/*
			This loops through our table structure and puts the data in the right order. Allows users
			to change the column order, or add/remove columns if they care to without resorting to the
			code. There will need to be a tool to change column order to make this valuable.
			*/	
		
			for(column in displayData.columns){
			
				colToDisplay = displayData.columns[column].displayColumn;
				colDisplayAlt = displayData.columns[column].displayAlt;
			
				displayVal = e[colToDisplay];
			
				if(displayVal == null || displayVal == undefined){ 
					displayVal = e[colDisplayAlt];
				}			
			
				var cell =	$(document.createElement("td"))
							.attr("id","item_"+e.itemid+"_"+displayData.columns[column].columnName)
							.addClass("item_"+displayData.columns[column].columnName);
			
				//Adds anything to the front of the cell content: Such as currency symbols.
				if(displayVal != undefined && displayVal.length > 0){
					cell.append(displayData.columns[column].prepend)
					.append(displayVal)
				}else{
					cell.append(displayData.columns[column].altDisplay);
				}
		
				// Add any important or relevant styles to the cell
				if(displayData.columns[column].displayStyles != undefined){
					cell.addClass(displayData.columns[column].displayStyles);
				}
				
				/*
					Below we handle any special cases, mainly extra items in columns for phone versions.
					
					TODO: Find a way to abstract these calls to output the data to an appropriate column without 
					defining the column or the extra items
					
					maybe: pass the displayColumn value, and clone specifics.
				*/
				if(displayData.columns[column].columnName == "Description"){
					var phoneTools = $(e.displayToolbox).clone(true).addClass("visible-phone");
					var phoneRanking = $(e.displayRanking).clone().addClass("visible-phone");
					cell.append(phoneTools, phoneRanking);
				}
				
				if(displayData.columns[column].columnName == "Price"){
					if(e.displayStatus != undefined){
						var phoneStatus = $(document.createElement('div'))
											.append(e.displayStatus)
											.addClass("visible-phone pull-right");
						cell.append(phoneStatus);
					}
				}
								
				//Finally append the cell to the row.
				row.append(cell);
			}
			
			table.append(row);
		});	
		
		//displayData.currentSort.call(this,displayData.list);
		
	}else{
		
		var row = $(document.createElement('tr'));
		var td = $(document.createElement('td'))
					.append("No items were found for this list")
					.attr("colspan",displayData.columns.length);
		row.append(td);
		
		table.append(row);
	}
}

function displayItemsDetails(itemInfo){

	$("#itemDetailName").html(itemInfo.itemid);
	$("#itemDetailComment").html(itemInfo.comment);
	
	$("#itemDetailsModal").modal('show');
}

/*
	Method: displayRanks
	
	Gets the list of ranks from the server and displays them on the manage tab.
	
*/
function displayRanks(){
	
}

function login(){

	userVal = jQuery("#username").val();
	passVal = jQuery("#password").val();

	if(userVal == null || userVal.length == 0 || passVal == null || passVal.length == 0){
		displayAlert("You must enter a username and password");
		return false;
	}

	data = {
		interact:'user',
		action:'loginUser',
		username: userVal, 
		password: passVal
	}
	
	showLoadingIndicator();
	
	jQuery.post('ajaxCalls.php',data,function(response){

		hideLoadingIndicator();

		if(response == "true"){
			window.location.reload();
		}else{
			displayAlert("Incorrect login");
		}
	});
}

function logout(){

	data = {
		interact:'user',
		action:'logoutUser'
	}
	
	jQuery.post('ajaxCalls.php',data,function(response){
		if(response){
			window.location.reload();
		}
	});
	
}

/*
	Method: requestResetPassword
	Sends a request to the system to reset the password and send an email to the user's email address
	
	@emailAddress - The email address to search for to reset the password.
	
*/
function requestResetPassword(emailAddress){
	data = {
		interact:'user',
		action:'requestPasswordReset',
		args:{'emailAddress':emailAddress}
	}
	showLoadingIndicator();
		
	jQuery.post('ajaxCalls.php',data,function(response){
			hideLoadingIndicator();
			if(response.responseType){
				$("#passwordRecovery").modal('hide');
				displayAlert(response.message);
			}
	},"json");
}




function getCurrentUserList(includeReceived){
	data = {
		interact:'wishlist',
		action:'getCurrentUserWishlist',
		args:{'includeReceived':includeReceived}
	}
	showLoadingIndicator();
	jQuery.post('ajaxCalls.php',data,function(response){
		
		//storedData.userWishlist = response;
		
		//storedData.userWishlist = {};
		storedData.userWishlist.isCurrentUser = true;
		storedData.userWishlist.forUserId = userId;		
		storedData.userWishlist.toolset = "edit";		
		storedData.userWishlist.list = response;		
		storedData.userWishlist.targetTable = "userWishlist";
		storedData.userWishlist.skipHeader = false;
		storedData.userWishlist.columns = storedData.columns;
		
		storedData.userWishlist = storedData.userWishlist;
		
		displayWishlist(storedData.userWishlist);
		if(storedData.userWishlist.list != null){
			storedData.userWishlist.currentSort.call(this,storedData.userWishlist);
		}
		
		hideLoadingIndicator();		
	},"json");
}


/*
Function: getUserWishlist
	fetches the wishlist of a particular user and displays it in the "otherUserWishlist" table.
	
	@forUserId - The id of the user you would like to get the list for.
*/
function getUserWishlist(forUserId){
	data = {
		interact:'wishlist',
		action:'getShoppingForList',
		args:{shopForId:forUserId}
	}
	showLoadingIndicator();
	jQuery.post('ajaxCalls.php',data,function(response){
		
		storedData.otherUserWishlist.list = "";
		
		if(response != null && response.responseType != undefined && response.responseType == "error"){
			errorMessage(response);
			//return false;
		}else{

			storedData.otherUserWishlist.isCurrentUser = false;
			storedData.otherUserWishlist.forUserId = forUserId;
			storedData.otherUserWishlist.toolset = "shop";
			storedData.otherUserWishlist.list = response;		
			storedData.otherUserWishlist.skipHeader = false;			
			storedData.otherUserWishlist.targetTable = "otherUserWishlist";
			storedData.otherUserWishlist.columns = storedData.columns2;
			
			displayWishlist(storedData.otherUserWishlist);
			if(storedData.otherUserWishlist.list != null){
				storedData.otherUserWishlist.currentSort.call(this,storedData.otherUserWishlist);
			}
		}
		hideLoadingIndicator();
	},"json");
}

/*
Javascript sort functions
*/

function sortByPriceDesc(listObject){
	listObject.currentSort = sortByPriceDesc;
	listObject.list.sort(function(a,b){return a.minprice - b.minprice});
	listObject.skipHeader = true;
	displayWishlist(listObject);
}

function sortByPriceAsc(listObject){
	listObject.currentSort = sortByPriceAsc;
	listObject.list.sort(function(a,b){return b.minprice - a.minprice});
	listObject.skipHeader = true;
	displayWishlist(listObject);	
}


function sortByDescriptionDesc(listObject){
	listObject.currentSort = sortByDescriptionDesc;	
	listObject.list.sort(function(a,b){
			if(a.description.replace(/['"]/g,"").toLowerCase() > b.description.replace(/['"]/g,"").toLowerCase()){
				return 1;
			}else if(a.description.replace(/['"]/g,"").toLowerCase() < b.description.replace(/['"]/g,"").toLowerCase()) {
				return -1;
			}else{
				return 0;
			}
		});
	listObject.skipHeader = true;
	displayWishlist(listObject);
}

function sortByDescriptionAsc(listObject){
	listObject.currentSort = sortByDescriptionAsc;
	listObject.list.sort(function(a,b){
		if(a.description.replace(/['"]/g,"").toLowerCase() > b.description.replace(/['"]/g,"").toLowerCase()){
			return -1;
		}else if(a.description.replace(/['"]/g,"").toLowerCase() < b.description.replace(/['"]/g,"").toLowerCase()) {
			return 1;
		}else{
			return 0;
		}
	});
	listObject.skipHeader = true;
	displayWishlist(listObject);
}

function sortByRankingAsc(listObject){	
	listObject.currentSort = sortByRankingDesc;
	listObject.list.sort(function(a,b){return a.ranking - b.ranking});
	listObject.skipHeader = true;
	displayWishlist(listObject);
}

function sortByRankingDesc(listObject){
	listObject.currentSort = sortByRankingAsc;
	listObject.list.sort(function(a,b){return b.ranking - a.ranking});
	listObject.skipHeader = true;
	displayWishlist(listObject);	
}



function sortByCategoryAsc(listObject){
	listObject.currentSort = sortByCategoryAsc;
	listObject.list.sort(function(a,b){
			if(a.displayCategory.toLowerCase() > b.displayCategory.toLowerCase()){
				return -1;
			}else if(a.displayCategory.toLowerCase() < b.displayCategory.toLowerCase()) {
				return 1
			}else{
				return 0;
			}
		});
	listObject.skipHeader = true;
	displayWishlist(listObject);
}

function sortByCategoryDesc(listObject){
	listObject.currentSort = sortByCategoryDesc;
	listObject.list.sort(function(a,b){
			if(a.displayCategory.toLowerCase() > b.displayCategory.toLowerCase()){
				return 1;
			}else if(a.displayCategory.toLowerCase() < b.displayCategory.toLowerCase()) {
				return -1
			}else{
				return 0;
			}
		});
	listObject.skipHeader = true;
	displayWishlist(listObject);
}



/*
	Method: generateDisplayElements
	This method takes a data object returned from the database and generates appropriate display data. 
	Returns the object
	If you want to add controls, images, etc to individual list items on the list, this is the place to do it.
	

	object @itemObject - The item returned from the database.
	
*/
function generateDisplayElements(itemObject){

	switch(itemObject.toolset){
		case "shop":
			itemObject.displayToolbox = renderItemTools(itemObject,"shop");

			itemObject.displayDescription = $(document.createElement("div"));		
			itemObject.displayDescription.append($(document.createElement("span")).append(itemObject.description))
			
			if(itemObject.available == null) itemObject.available = itemObject.quantity;
			itemObject.displayStatus = (itemObject.available > 0)? itemObject.available+"/"+itemObject.quantity+" Remaining":"None Remaining";
			
			
		break;
		case "edit":
		
			itemObject.displayToolbox = renderItemTools(itemObject,"edit");
			itemObject.displayDescription = $(document.createElement("span")).append(itemObject.description);
		break;
	}
	
	itemObject.displayRanking = renderRanking(itemObject.ranking);

	return itemObject
}



function showMoreInfo(eventObject){
	//infoContainer = jQuery("#itemDetailRow");
	itemRow = jQuery(eventObject.target).closest('tr');
	//itemRow.after(infoContainer);
	
	getItemDetailInfo(itemRow.attr("data-itemid"));
}


/*
	Method: getItemDetailInfo
	This method takes an itemId and fetches detailed information about it: Images, Sources (shops), Reservation info, and Comments.

	int @itemId - the id of the item to be requested.

*/
function getItemDetailInfo(itemId){
	//jQuery(".itemDetailContainer").html("");
	
	data = {
		interact:'wishlist',
		action:'getItemDetails',
		args:{itemid:itemId}
	}
	showLoadingIndicator();
	
	jQuery.post('ajaxCalls.php',data,function(response){
		
		debug = response;
		jQuery('#itemDetailName').html(response.itemDescription);
		jQuery('#itemDetailComment').html(response.itemComment);
		jQuery('#itemDetailRanking').html(renderRanking(response.itemRanking));
		
		
		//Sources Section
		if(response.sources != null && response.sources != undefined){
			jQuery("#itemDetailSourcesTable").html("");
		
			jQuery(response.sources).each(function(i,e){
			sourceRow = jQuery(document.createElement('tr'));
			sourceNameCell = jQuery(document.createElement('td'));
			sourcePriceCell = jQuery(document.createElement('td'));
			
			//Add a url source or just the sourcename.
			if(e.itemSourceUrl != null){ 
				sourceName = jQuery(document.createElement('a'))
								.attr('href',e.itemSourceUrl)
								.attr("target","_blank")
								.append(e.itemSource);
			}else{
				sourceName = e.itemSource;
			}
			
			sourceNameCell.append(sourceName);
			sourcePriceCell.append(storedData.currencySymbol+e.itemSourcePrice);
			
			sourceRow.append(sourceNameCell)
					.append(sourcePriceCell);
			
			jQuery("#itemDetailSourcesTable").append(sourceRow);
		});
		
		}else{
			jQuery('#itemDetailSourcesTable').html("No Stores/Shops have been provided for this item.");
		}

		//Images Section
		if(response.images != undefined && response.images != null){
			
			//Clears out previous carousel items
			jQuery('#itemDetailImageCarousel div.carousel-inner').empty();
			
			//Generates carousel images & containers.
			jQuery(response.images).each(function(i,e){
				img = $(document.createElement('img')).attr('src',storedData.filepath+e.itemImageFilename);
				itemImageDiv = $(document.createElement('div')).addClass("item").append(img);
				if(i == 0)itemImageDiv.addClass("active");
				jQuery('#itemDetailImageCarousel div.carousel-inner').append(itemImageDiv);
			});
			
			//Starts the carousel.
			jQuery('#itemDetailImageCarousel').carousel();
			
		}else{
			jQuery('#itemDetailImageCarousel div.carousel-inner').html("No images have been provided for this item.");
		}
		
		//Alloc Section
		allocElement = jQuery(document.createElement("div"));
		
		
		if(response.allocs != null &&  response.allocs != "currentUser"){
			jQuery(response.allocs).each(function(i,e){
				message = e.itemAllocUserName+" has reserved "+e.itemAllocQuantity+" of this item";
				allocElement.append($(document.createElement('div')).html(message));				
			});
		}else if(response.allocs == null){
		    allocElement.html("This item has not be reserved yet.");
		}

		jQuery("#itemDetailAlloc").html(allocElement);
		
		$("#itemDetailsModal").modal();
		
		hideLoadingIndicator();
	},"json");	
	
}


/*
Function: renderRanking
	Currently takes an integer and turns it into a series of asterisks. In the future this should render an 
	image.
	
	int @rankValue - The ranking of an item as a number.
*/
function renderRanking(rankValue){
	var rankReturn = $(document.createElement('div')).addClass('rank');
	var myClass = "";
	
	switch(rankValue){
		case "1":
			myClass = "one";
		break;
		case "2":
			myClass = "two";		
		break;
		case "3":
			myClass = "three";		
		break;
		case "4":
			myClass = "four";		
		break;
		case "5":
			myClass = "five";
		break;
	}
	
	rankReturn.addClass(myClass);
	return rankReturn;
}

/*

-----DEPRECATED! RenderCategory is currently doing nothing!-------------

Function renderCategory
	Takes a category id and returns the category display name. Fetches the category set and populates the storedData.categories object if it's empty.
	
	int @categoryId - the id (interger) of the category
*/
/*
function renderCategory(categoryId){
	if(storedData.categories.length == 0){
		console.log("preparing to request data");
		data = {
			interact:'wishlist',
			action:'getCategories'
		}
		jQuery.post('ajaxCalls.php',data,function(response){
			storedData.categories = response;
			return storedData.categories[categoryId];
		},"json");
	}else{
		console.log("data is local");
	}
}
*/

/*
Function renderItemTools
	Produces HTML buttons/icons for interacting with the item in the row.
	
	JS Object @itemObject - A Javascript object returned from the wishlist system.
	JS Object @toolInfo - A Javscript object with owner,  
*/
function renderItemTools(itemObject, toolInfo){
	
	toolBox = $(document.createElement("div"));
	
	switch(toolInfo){
		
		case "edit":		
			itemDelete = $(document.createElement("i"))
							.addClass("icon-trash tool")
							.attr("title","Delete Item");
							
			itemReceive = $(document.createElement("i"))
							.addClass("icon-gift tool")
							.attr("title","Mark Item Received");
							
			itemEdit = $(document.createElement("i"))
							.addClass("icon-pencil tool")
							.attr("title","Edit Item");
			
			
			if(storedData.largeIcons){
				itemDelete.addClass("icon-large");
				itemReceive.addClass("icon-large");
				itemEdit.addClass("icon-large");
			}
			

			itemReceive.click(function(){
				displayConfirmScreen($(this).closest("tr").attr("data-itemId"),"item","markReceived")
			});

			itemEdit.click(function(){
				populateManageItemForm($(this).closest("tr").attr("data-itemId"));
			});

			itemDelete.click(function(){
				displayConfirmScreen($(this).closest("tr").attr("data-itemId"),"item","deleteItem")				
			});

			toolBox.append(itemDelete);	
			toolBox.append(itemEdit);
			toolBox.append(itemReceive);			
		break;
		case "shop":

			//Reserve
			if(itemObject.available > 0 ){	
				itemReserve = $(document.createElement("i"))
						.addClass("icon-lock tool")
						.attr("title","Reserve Item")
						.click(function(){
							allocateHandler($(this).closest("tr").attr("data-itemId"),userId,"reserve", $(this).closest("table").attr("data-listOwner"));
						});
				
				if(storedData.largeIcons){
					itemReserve.addClass("icon-large");
				}			
						
				toolBox.append(itemReserve);
			}
		
			if(itemObject.reservedByThisUser > itemObject.boughtByThisUser){
				//Release
				itemRelease = $(document.createElement("i"))
						.addClass("icon-hand-left tool")
						.attr("title","Release Item")
						.click(function(){
							allocateHandler($(this).closest("tr").attr("data-itemId"),userId,"release", $(this).closest("table").attr("data-listOwner"));
						});
				if(storedData.largeIcons){
					itemRelease.addClass("icon-large");
					itemRelease.removeClass("icon-hand-left").addClass("icon-unlock");
				}							
				toolBox.append(itemRelease);

				//Purchase
				itemBuy = $(document.createElement("i"))
						.addClass("icon-shopping-cart tool")
						.attr("title","Buy Item")
						.click(function(){
							allocateHandler($(this).closest("tr").attr("data-itemId"),userId,"purchase", $(this).closest("table").attr("data-listOwner"));
						});
				if(storedData.largeIcons){
					itemBuy.addClass("icon-large");
				}	
				toolBox.append(itemBuy);
				
			}
			
			//Return
			if(itemObject.boughtByThisUser > 0){
				itemReturn = $(document.createElement("i"))
						.addClass("icon-share tool")
						.attr("title","Return Item")
						.click(function(){
							allocateHandler($(this).closest("tr").attr("data-itemId"),userId,"return", $(this).closest("table").attr("data-listOwner"));
						});
				if(storedData.largeIcons){
					itemReturn.addClass("icon-large icon-cart-out").removeClass("icon-share");
				}
				
				toolBox.append(itemReturn);
			}
			

			//There are no limitations on copying an item. 
			itemCopy = $(document.createElement("i"))
					.addClass("icon-more-items tool")
					.attr("title","Copy Item")
					.click(function(){
						copyItem($(this).closest("tr").attr("data-itemId"));
					});
					
			if(storedData.largeIcons){
				itemCopy.addClass("icon-large");
			}							
			toolBox.append(itemCopy);

		break;
		case "sourceEdit":
		
			sourceDelete = $(document.createElement("i"))
							.addClass("icon-trash tool")
							.attr("title","Delete Source");
						
			sourceEdit = $(document.createElement("i"))
							.addClass("icon-pencil tool")
							.attr("title","Edit Source");

			if(storedData.largeIcons){
				sourceDelete.addClass("icon-large");
				sourceEdit.addClass("icon-large");
				//sourcePrimary.addClass("icon-large");
			}
			
			sourceEdit.click(function(){
				populateItemSourceForm($(this).closest("tr").attr("data-itemSourceId"));
			});

			sourceDelete.click(function(){
				deleteItemSource(
					$("input#itemId").val(),
					$(this).closest("tr").attr("data-itemSourceId")
				);
			});

			toolBox.append(sourceDelete);
			toolBox.append(sourceEdit);
			//toolBox.append(sourcePrimary);
		
		break;		
	}
	/*
		Items that are included in the toolbox regardless
	*/
	if(toolInfo != "sourceEdit"){
		var detailsButton = $(document.createElement("i"))
					.addClass("icon-large icon-circle-info tool")
					.click(function(){
						getItemDetailInfo($(this).closest("tr").attr("data-itemId"));
					});
	
		toolBox.append(detailsButton);
	}
	return toolBox;
	
}

/*
Function buildShopForSet
	Builds HTML set items for list selection and list of users a user is shopping for.
*/

function buildShopForSet(){
	data = {
		interact:'user',
		action:'getShopForUsers'
	}
	
	jQuery.post('ajaxCalls.php',data,function(response){
		listOfUsersTable = $("#listOfUsersTable");
		shopForTable = $("#currentShopFor");
		
		var messageRecipients = $("select#messageRecipient");
		messageRecipients.empty();
		
		shopForTable.empty();
		
		$(response).each(function(i,e){
			shopForRow = $(document.createElement("tr"));
			shopForCell = $(document.createElement("td"))
							.html(e.fullname)
							.attr("data-userid",e.userid);
			if(e.pending == 1){
				shoppingForState = $(document.createElement('span')).html("Pending").addClass("label label-warning cellInfo");

			}else{
				shoppingForState = $(document.createElement('button')).html("Remove")
							.addClass("btn btn-mini btn-danger cellInfo")
							.click(function(){
								removeShoppingFor($(this).closest('td').attr("data-userid"));
							});				
			}
			shopForCell.append(shoppingForState);
						
			shopForRow.append(shopForCell);
			shopForTable.append(shopForRow);			

			if(e.pending == 0){
				userRow = $(document.createElement("tr"));
				nameCell = $(document.createElement("td"))
							.html(e.fullname)
							.attr("data-userid",e.userid)
							.attr("id","listRowUser-"+e.userid);
			
				nameCell.click(function(e){
					usersName = $(e.target).html(); //As long as the name is the only thing here, we can just grab the cell contents.
					var forUserId = $(e.target).attr("data-userid");
					
					getUserWishlist(forUserId);
					
					//Sets the request to fire the slideshow transition onclick.
					$("#otherWishlistTitle").html("Wishlist for "+usersName);
					$("#myCarousel").carousel('next');
				});
			
				userRow.append(nameCell);			
				listOfUsersTable.append(userRow);
			}
			
			var recipient = $(document.createElement("option")).val(e.userid).html(e.fullname);
			messageRecipients.append(recipient);
			
		});
		
		
		$("#listOfUsers").change(function(e){
				getUserWishlist(this.value);
			});
		
	},"json");
}


/*
	Method: buildCategorySelect
		Builds option elements with category names and their ids as values. Appends them to the second argument Element.
		
		array @categoryObject - a javascript array of objects that contain category names and ids.
		string @parentElement - the element where these items should be appened to.
*/
function buildCategorySelect(){

	//Set these to defaults if they're not defined in the call
	//I don't think these are necessary any longer because this is no longer generated on the fly.

	var table = $("table#categoriesTable").empty();

	jQuery(storedData.categories).each(function(i,e){
		var option =  jQuery(document.createElement("option"))
							.attr("value",e.categoryid)
							.html(e.category);
		jQuery("select#itemCategoryInput").append(option);
				
		var editCategory = $(document.createElement('button'))
								.append("edit")
								.addClass("btn btn-mini pull-right")
								.click(function(){
									$("input#categoryId").val(e.categoryid);
									$("input#categoryName").val(e.category);
									$('div#manageCategoryFormBlock').modal('show');
								});
								
		var catTd = $(document.createElement('td'))
						.append(e.category,editCategory);

		var row = $(document.createElement('tr'))
						.append(catTd);
		
		table.append(row);
		
	});
		
}

/*
	Method: remoteBuildCategorySelect
	Allows for the remote update of categories object and then update of categories listings.
*/

function remoteBuildCategorySelect(){
	data = {
		interact:'wishlist',
		action:'getCategories'
	}
	
	//Get the Categories.
	jQuery.post('ajaxCalls.php',data,function(response){	
		storedData.categories = response;
		buildCategorySelect();
	},"json");
}



/*
	Method: buildRankSelect
		Builds option elements with Rank display, Appends them to the second argument Element. Depends on renderRanking

		array @rankObject - a javascript array of objects that contain category names and ids.
		string @parentElement - the element where these items should be appened to.
*/
function buildRankSelect(){
	var rankOptionsList = "";
	
	data = {
		interact:'wishlist',
		action:'getRanks'
	}
	
	//Get the Categories.
	jQuery.post('ajaxCalls.php',data,function(response){
		var select = $("select#itemRankInput");
		var table = $("table#rankingsTable").empty();
		
		$(response).each(function(i,e){
			 var rankOption = $(document.createElement('option')).val(e.ranking).html(e.title);
			 select.append(rankOption);
			
			var editRank = $(document.createElement('button'))
								.append("edit")
								.addClass("btn btn-mini pull-right")
								.click(function(){
									$("input#rankId").val(e.ranking);
									$("input#rankTitle").val(e.title);
									$('div#manageRankFormBlock').modal('show');
								});
								
			var rankTd = $(document.createElement('td')).append(e.title,editRank);
			var row = $(document.createElement('tr')).append(rankTd);	
			
			table.append(row);
		});
					
	},"json");

}



/*
	Method: deleteItem
		Deletes an item from the user's wishlist
		
		int @itemId - The id of the item to delete.
*/
function deleteItem(itemId){
	
	data = {
		interact:'wishlist',
		action:'manageItem',
		args:{
			itemAction:'delete',				
			itemid:itemId
		}
	}
	showLoadingIndicator();
	
	//Get the Categories.
	jQuery.post('ajaxCalls.php',data,function(response){
		hideLoadingIndicator();
		if(response){
			getCurrentUserList(listReceived);
			$("#confirmActivityBlock").modal('hide');
		}
		
	});
}

/*
	Method: markItemReceived
		Marks an item as received
		
		int @itemId - The id of the item to delete.
*/
function markItemReceived(itemId){
	
	data = {
		interact:'wishlist',
		action:'markItemReceived',
		args:{
			itemid:itemId
		}
	}
	showLoadingIndicator();
	//Get the Categories.
	jQuery.post('ajaxCalls.php',data,function(response){
		hideLoadingIndicator();
		if(response){
			getCurrentUserList(listReceived);
			$("#confirmActivityBlock").modal('hide');
		}
		
	});
}




/*
	Method: copyItem
	Copies an item to a particular user.
	
	int @itemid - The ID of the item to be copied.
*/
function copyItem(itemId){
	data = {
		interact:'wishlist',
		action:'copyItem',
		args:{
			"itemid":itemId,
			"userid":userId
		}
	}
		showLoadingIndicator();
	jQuery.post('ajaxCalls.php',data,function(response){
		hideLoadingIndicator();
		
		getCurrentUserList(listReceived);
	},"json");	
	
	
}



/*	Method: manageItem
	Takes data from manageItemForm and sends it to the database via AJAX
				
*/
function manageItem(){
	
	data = {
		interact:'wishlist',
		action:'manageItem',
		args:{}
	}
	currentItemId = jQuery("#manageItemForm #itemId").val();
	
	//Ternary operation to determine whether we're editing or adding an item.
	data.args.itemAction = (currentItemId == "") ? "add" : "edit";
	
	data.args.itemid = currentItemId;
	data.args.description = jQuery("#itemDescriptionInput").val();
	data.args.category = jQuery("#itemCategoryInput").val();
	data.args.quantity = jQuery("#itemQuantityInput").val();
	data.args.comment = jQuery("#itemCommentInput").val();
	data.args.ranking = jQuery("#itemRankInput").val();
	
	jQuery.post('ajaxCalls.php',data,function(response){
		
		getCurrentUserList(listReceived);
		$("#manageItemFormBlock").modal('hide');
	});	
}

/* Method: clearManageItemForm
	Clears unique inputs on manageItemForm
	
*/
function clearManageItemForm(){
	jQuery('#itemId').val("");
	jQuery('#itemDescriptionInput').val("");
	jQuery('#itemQuantityInput').val("");
	jQuery('#itemRankInput').val("1");
	jQuery('#itemCategoryInput').val("1");
	
	//jQuery('#itemSourcesEdit').html("");
	
	jQuery('#itemSourcesEdit tr.sourceRow').remove();
	
	jQuery('#itemCommentInput').val("");	
	jQuery('#openAddImageForm').attr("data-forItemId","");	
	
	//console.log("form cleared");
}

/* Method: populateManageItemForm
	Gets the itemDetails and puts them into the #manageItemForm form, then calls the modal to display.
*/
function populateManageItemForm(itemId){
	
	clearManageItemForm(); //Clears the form of any previous data. 
	
	data = {
		interact:'wishlist',
		action:'getItemDetails',
		args:{itemid:itemId}
	}
	showLoadingIndicator();

	jQuery.post('ajaxCalls.php',data,function(response){
	
		jQuery('#itemId').val(itemId);
		jQuery('#itemDescriptionInput').val(response.itemDescription);
		jQuery('#itemQuantityInput').val(response.itemQuantity);
		jQuery('#itemRankInput').val(response.itemRanking);
		jQuery('#itemCategoryInput').val(response.itemCategory);
		jQuery('#itemCommentInput').val(response.itemComment);		
		jQuery('#openAddImageForm').attr("data-forItemId",itemId);
		
		//Sources Data
		if(response.sources != undefined){
			jQuery(response.sources).each(function(i,e){
				
				sourceCell = jQuery(document.createElement('td'))
								.html(e.itemSource);

				
				sourceEditTools = jQuery(document.createElement('td'))
										.append(renderItemTools(e,"sourceEdit"));
									
								
				sourceOption = jQuery(document.createElement('tr'))
									.append(sourceCell)
									.append(sourceEditTools)
									.attr("data-itemSourceId",e.itemSourceId)
									.addClass("sourceRow");
		
				jQuery("tr#addSourceRow").before(sourceOption);
			});
		
		}

		$("#openAddImageForm").removeClass("disabled").prop("disabled","");
		$("#addSourceButton").removeClass("disabled").prop("disabled","");
		
		$('#manageItemFormBlock').modal('show');
		hideLoadingIndicator();
	},"json");
}

/* Method clearItemSourceForm
	Clears the values in the Item Source Form
*/
function clearItemSourceForm(){
	
	$("#itemSourceForm #itemId").val("");
	$("#itemSourceForm #sourceId").val("");
	$("#itemSourceForm #sourceName").val("");
	$("#itemSourceForm #sourceUrl").val("");
	$("#itemSourceForm #sourcePrice").val("");
	$("#itemSourceForm #sourceComments").val("");
	
}



/* Method populateItemSourceForm
	Gets itemSource details and puts them into the #itemSourceForm form, then calls the modal to display.
*/
function populateItemSourceForm(sourceId){
	// Note to developer: This should probably also get the name of the related item, so it's available to the user for context.
	data = {
		interact:'wishlist',
		action:'getSourceDetails',
		args:{'sourceId':sourceId}
	}
	showLoadingIndicator();
	
	jQuery.post('ajaxCalls.php',data,function(response){
		debug = response;
		
		$("#itemSourceForm #sourceItemId").val(response.itemid);
		$("#itemSourceForm #sourceId").val(response.sourceid);
		$("#itemSourceForm #sourceName").val(response.source);
		$("#itemSourceForm #sourceUrl").val(response.sourceurl);
		$("#itemSourceForm #sourcePrice").val(response.sourceprice);
		$("#itemSourceForm #sourceComments").val(response.sourcecomments);										
		
		hideLoadingIndicator();
		
		$('#manageItemFormBlock').modal('hide');
		$("#itemSourceFormBlock").modal("show").on('hide',function(){
			$("#manageItemFormBlock").modal('show');
		});
	
	},"json");	
}


/* Method: manageItemSource
	Adds or Updates an item source.

*/
function manageItemSource(){
	
	args = {};
	
	args.itemid = $("#itemSourceForm #sourceItemId").val();
	args.sourceid = $("#itemSourceForm #sourceId").val();
	args.source = $("#itemSourceForm #sourceName").val();
	args.sourceurl = $("#itemSourceForm #sourceUrl").val();
	args.sourceprice = $("#itemSourceForm #sourcePrice").val();
	args.sourcecomments = $("#itemSourceForm #sourceComments").val();
	args.itemSourceAction = (args.sourceid.length == 0)?"add":"edit";

	data = {
		interact:'wishlist',
		action:'manageItemSource',
		"args":args
	}
	
	jQuery.post('ajaxCalls.php',data,function(response){
		if(response){
			$("#itemSourceFormBlock").modal("hide");
			getCurrentUserList(listReceived);
			populateManageItemForm(args.itemid);
			
		}
	},"json");
	
}


/* Method: deleteItemSource
	removes a source from an item
*/
function deleteItemSource(itemId,sourceId){
	data = {
		interact:'wishlist',
		action:'manageItemSource',
		args:{'sourceid':sourceId,
			'itemSourceAction':'delete'}
	}
	
	jQuery.post('ajaxCalls.php',data,function(response){
		populateManageItemForm(itemId);
	});	
}


function populateImagesOnForm(itemId){
	data = {
		interact:'wishlist',
		action:'getImages',
		args:{"itemId":itemId}
	}
	jQuery.post('ajaxCalls.php',data,function(response){
		$("#currentImagesBlock").empty();
		
		$(response).each(function(i,e){
			var img = $(document.createElement('img')).attr('src',storedData.filepath+e.filename);
			var itemImageDiv = $(document.createElement('div')).append(img).addClass("imageBlock");
			//
			var removeImageControl =  $(document.createElement('a')).addClass("close").append("×").click(function(){
				
				remove = {
					interact:'wishlist',
					action:'manageItemImage',
					args:{
						"imageid":e.imageid,
						"itemImageAction":"delete"
					}
				}
				jQuery.post('ajaxCalls.php',remove,function(response){
					$(this.parent).remove();
					//This seems recursive, but it's really just setting up the call to refresh the images on the form here.
					populateImagesOnForm(itemId); 
				});
			});
			
			itemImageDiv.append(removeImageControl);
			$("#currentImagesBlock").append(itemImageDiv);
		});

	},"json");
}

/*
	Method populateManageUserForm
		Populates the Manage User Form.
*/
function populateManageUserForm(userId){
	data = {
		interact:'user',
		action:'getUser',
		args:{"userid":userId}
	}
	showLoadingIndicator();
	jQuery.post('ajaxCalls.php',data,function(response){
		
		
		$("#manageUser input#userId").val(userId);
		$("#username").val(response.username);
		$("#userFullName").val(response.fullname);
		$("#emailAddress").val(response.email);

		//Any time we're populating this form, we're going to be editing, so we can put this in the function call.
		$("#manageUser input#userAction").val("edit"); 

		//We need some logic for these when the logged in user is not an Admin, and these elements don't exist.
		var userApproved = (response.approved == 1)?true:false;
		var userAdmin = (response.admin == 1)?true:false;
		var userEmailMsg = (response.email_msgs == 1)?true:false;
		
		
		$("#userApproved").prop("checked",userApproved);
		$("#userIsAdmin").prop("checked",userAdmin);
		$("#emailMessages").prop("checked",userEmailMsg);
		
		$("#userFormBlock").modal();
		
		hideLoadingIndicator();
		
	},"json");
	
}

/*
	Method updateUserData
		Updates 
*/
function updateUserData(){
	args = {};
	data = {
		interact:'user',
		action:'manageUser'
	}

	fields = {
		"username":"#manageUser #username",
		"fullname":"#manageUser #userFullName",
		"email":"#manageUser #emailAddress",
	}
	
	for(fieldName in fields){
		args[fieldName] = $(fields[fieldName]).val();
	}

	//var emailCheck = $("#manageUser #emailMessages:checked");
	
	var approvedCheck = $("#manageUser #userApproved:checked");
	var adminCheck = $("#manageUser #userIsAdmin:checked");


	for(field in args){
		if(args[field].length == 0){
			//If the field has a length 0, we set the help text, and attach the error class to the parent control-group
			$(fields[field]).next(".help-inline").append("Field must not be empty")
							.closest(".control-group").addClass("error");
			return false;	
		}
	}

	args.emailMsg = ($("#manageUser #emailMessages").prop("checked"))? 1:0;
	
	if(approvedCheck != undefined && approvedCheck.length > 0){
		args.approved = 1;
	}
	
	if(adminCheck != undefined && adminCheck.length > 0){
		args.admin = 1;
	}
		
	var pass = $("#manageUser #userPassword").val();
	var confirmPass = $("#manageUser #userPasswordConfirm").val();

	if(confirmPass == pass){
		args.password = pass;
	}else{
		$("#manageUser .passwordSet").closest(".control-group").addClass("error");
		$("#userPassword").next(".help-inline").append("Both password fields must match");
		
		// This will set the interface to remove the error class and warning on clicking the field.
		$("#manageUser .passwordSet").click(function(){
			$("#manageUser .passwordSet")
				.unbind("click")
				.next(".help-inline")
					.html("")
				.closest(".control-group")
					.removeClass("error");
		});
		return false;
	}
	args.userId = $("#manageUser #userId").val();
	args.userAction =$("#manageUser #userAction").val();
	
	
	//If we've made it this far, we're packaging up and sending off!
	data.args = args;	
	jQuery.post('ajaxCalls.php',data,function(response){
		if(response.type != undefined && response.type == 'error'){
			displayAlert(response.message);
		}
		
		$("#userFormBlock").modal("hide");
	},"json");
	
}



/*
	method deleteUserFromSystem

*/
function deleteUserFromSystem(userId){
	data = {
		interact:"user",
		action:'manageUser',
		args:{"userAction":"delete","userId":userId}
		
	}
	jQuery.post('ajaxCalls.php',data,function(response){
		if(response){
			$("#confirmActivityBlock").modal('hide');
			displaySystemUsers();
		}
	});
}


/*
	Method displaySystemUsers
		Gets a list of users in the system. Only works for Admins.
*/
function displaySystemUsers(){
	data = {
		interact:'user',
		action:'getListOfUsers'
	}
	
	jQuery.post('ajaxCalls.php',data,function(response){
		
		var userListTable = $("table#adminUserList");
		userListTable.empty();
		for(user in response){
			
			var row = $(document.createElement("tr")).attr("data-userId",response[user].userid);
			var name = $(document.createElement("td")).append(response[user].fullname);
			var userName = $(document.createElement("td")).append(response[user].username);
			var email = $(document.createElement("td")).append(response[user].email);
			
			//We should probably expand on tool construction above - but barring that here are some quick tools for management.
			var editUserButton = $(document.createElement("button"))
									.addClass("btn btn-info btn-mini pull-right")
									.append("Edit user")
									.click(function(){
										
										populateManageUserForm($(this).closest("tr").attr("data-userId"));
									});
									
			var deleteUserButton = $(document.createElement("button"))
										.addClass("btn btn-danger btn-mini pull-right")
										.append("Delete user")
										.click(function(){
											//Set the form delete ID to the userId provided and show the warning Modal.
											$("#deleteObjectForm #deleteObjectId").val($(this).closest("tr").attr("data-userId"));
											displayConfirmScreen($(this).closest("tr").attr("data-userid"),"user","deleteUser");
										});
									
									
									
			
			var userTools = $(document.createElement("td")).append();
			userTools.append(deleteUserButton)
						.append(editUserButton);
			
			row.append(name)
				.append(userName)
				.append(email)
				.append(userTools);
			
			userListTable.append(row);
		}
	},"json");
	
}

/*
	Method allocateHandler
		manages calls to allocating Methods on backend.
*/
function allocateHandler(itemId,userId,action,forUserId){

	arguments = {
		"userid":userId,
		"itemid":itemId,
		"allocateAction":action,
		"adjustment":1
	}
	
	data = {
		interact:'wishlist',
		action:'adjustReservedItem',
		"args":arguments
	}
	showLoadingIndicator();
	jQuery.post('ajaxCalls.php',data,function(response){
		hideLoadingIndicator();
		getUserWishlist(forUserId);
	});
}


/*
	Method setupUserSearch
		Gets all users, associated IDs, sets up array for searching and sets up user search field.
		
*/
function setupUserSearch(){
	data = {
		interact:'user',
		action:'getListOfUsers'
	}
	
	jQuery.post('ajaxCalls.php',data,function(response){
		storedData.userlist = response;
		
		$('#shopForSearch').typeahead({
			source:storedData.userlist,
		    onselect: function(obj){
				//requestToShopFor(userId,obj.userid)
				$("input#userToRequest").val(obj.userid);
			},
			property:'fullname'
		});		
	},"json");	
}




/*
	Method requestToShopFor
		Requests that a user may shop for the current user.
		
		@userid - The userId of the user making the request
		@shopForId - The user to be shopped for.
*/
function requestToShopFor(userId,shopForId){
	data = {
		interact:'user',
		action:'requestShopForUser',
		args:{'shopperId':userId,
			'shopForUserId':shopForId
		}
	}
	showLoadingIndicator();
	jQuery.post('ajaxCalls.php',data,function(response){	
		hideLoadingIndicator();
		buildShopForSet();
	},"json");
}

/*
	Method removeShoppingFor
		Removes a user from your 'shopping for' set.
*/
function removeShoppingFor(shopForId){
	//	$(this).closest('td').attr("data-userid")
	data = {
		interact:'user',
		action:'removeShopForUser',
		args:{'shopperId':userId,
			'shopForUserId':shopForId
		}
	}
	jQuery.post('ajaxCalls.php',data,function(response){	
		buildShopForSet();
	},"json");
	
}



/*
	Method approveShopper
		Approves a shopper's request to shop for you
*/
function approveShopper(shopperId){

	data = {
		interact:'user',
		action:'approveShopForUser',
		args:{'shopForId':userId,
			'shopperId':shopperId
		}
	}
	showLoadingIndicator();
	jQuery.post('ajaxCalls.php',data,function(response){
		hideLoadingIndicator();
		displayShopForMeList();
	},"json");
}

/*
	Method approveShopper
		Approves a shopper's request to shop for you
*/
function disapproveShopper(shopperId){

	data = {
		interact:'user',
		action:'disapproveShopForUser',
		args:{'shopForId':userId,
			'shopperId':shopperId
		}
	}
	jQuery.post('ajaxCalls.php',data,function(response){	
		displayShopForMeList();
	},"json");
}


/*
	Method displayShopForMeList
	Displays the list of users shopping for current user in the manage panel.
	
*/
function displayShopForMeList(){
	data = {
		interact:'user',
		action:'getUsersShoppingFor'
	}
	jQuery.post('ajaxCalls.php',data,function(response){	

		shopForMeTable = $("#shoppingForMe");
		shopForMeTable.empty();

		$(response).each(function(i,e){
			shopForMeRow = $(document.createElement("tr"));
			shopForMeCell = $(document.createElement("td"))
							.html(e.fullname)
							.attr("data-userid",e.userid);
	
				actionButton = $(document.createElement("button")).addClass("btn btn-mini cellInfo");
	
			if(e.pending == 1){
				actionButton.html("Approve")
							.addClass("btn btn-mini btn-info cellInfo")
							.click(function(){
								approveShopper($(this).closest('td').attr("data-userid"));
							});
			}else{
				actionButton.html("Remove")
							.addClass("btn-danger")
							.click(function(){
								disapproveShopper($(this).closest('td').attr("data-userid"));
							});
			}

			shopForMeCell.append(actionButton);			
			shopForMeRow.append(shopForMeCell);
			shopForMeTable.append(shopForMeRow);	
		});

	},"json");	
}

/*
	Method getMessagesForUser
	Gets a list of messages for the given user and displays them in the 
		@userId - The ID of the user
	
*/
function getMessagesForUser(userId,readStatus){
	data = {
		interact:'user',
		action:'getMessages',
		args:{
			"userid":userId,
			"readStatus":readStatus
		}
	}
	jQuery.post('ajaxCalls.php',data,function(response){	
		table = $("#userMessages");
		table.empty();
		
		if(response != null){
			//Adds a message count to the tab control - there are issues with clicking on the badge itself, so holding off for now.
			/*
			messageCount = $(document.createElement("span")).attr("id","messageIcon").addClass("badge badge-important").append('<i class="icon-envelope icon-white"></i>');
			$("#manageTab").append("&nbsp;").append(messageCount);
			*/
			
			$(response).each(function(i,e){
				debug = response;
				
				messageRow = $(document.createElement('tr'));
				messageCell = $(document.createElement('td'));
				trimmedMessage = e.message.substring(0,40);
				fullMessage = $(document.createElement('span'))
								.attr("id","message_"+e.messageid)
								.addClass("fullMessage")
								.append(e.message);
				
				fromLabel = $(document.createElement('span')).addClass("label pull-right");
				if(e.fullname == null){
					fromLabel.addClass("label-inverse").append("System");
				}else{
					fromLabel.addClass("label-info").append(e.fullname);
				}
				
				messageCell.append(trimmedMessage+"...");
				messageCell.append(fullMessage);
				messageCell.append(fromLabel);
				messageRow.append(messageCell);
				messageCell.click(function(ev){
					displayMessage($(this).children("span.fullMessage").html());
					markMessageRead(e.messageid);
				});

				table.append(messageRow);
			})
			$("#messageIndicator").show();
		}else{
			messageRow = $(document.createElement('tr'));
			messageCell = $(document.createElement('td')).append("No messages at this time");
			messageRow.append(messageCell);
			table.append(messageRow);	
			$("#messageIndicator").hide();
		}
	},"json");
}

/*
	Method displayMessage
	Displays a message from a message list in a modal.
	
	@messageBody - The body of the message you'd like displayed.
*/
function displayMessage(messageBody){
	$("#message div.modal-body").html(messageBody);
	$("#message").modal('show');	
}
/*
	Method markMessageRead
	Sets a message's status to read. Affects how it will be displayed in interface.
*/
function markMessageRead(messageId){
	data = {
		interact:'user',
		action:'markMessageRead',
		args:{
			"messageId":messageId
		}
	}
	jQuery.post('ajaxCalls.php',data,function(response){
		getMessagesForUser(userId,0);
	},"json");
}


/*
	Method displayConfirmScreen
	Displays confirmation screen for marking an item received, delete an item or deleting a user.
	
*/
function displayConfirmScreen(objectId,objectType,action){
	$("a.confirmButton").hide();

	$("#confirmObjectId").val("").val(objectId);
	$("#confirmType").val("").val(objectType);
	
	switch(action){
		case "deleteItem":
			$("#deleteSubmit").show();
			$("#confirmWarningMessage").html("Are you sure you want to delete this item? This cannot be undone.");		
		break;
		case "markReceived":
			$("#receivedSubmit").show();
			$("#confirmWarningMessage").html("Are you sure you want to mark this item as received? You may mark it as not-received in the manage screen later.");
		break;
		case "deleteUser":
			$("#deleteUserSubmit").show();
			$("#confirmWarningMessage").html("Warning! You are deleting a user on the system. If you continue, they will not be able to log in or access any of their items. A Deleted user cannot be recovered!");
		break;
	}
	$("#confirmActivityBlock").modal('show');
}

/*
	Method sendMessage
	Sends a message to a given recipient.
*/

function sendMessage(){
	var msgText = $("#messageText").val();
	var msgRecipient = $("#messageRecipient").val();
	
	data = {
		interact:'db',
		action:'sendMessage',
		args:{
			"senderId":userId,
			"receiverId":msgRecipient,
			"message":msgText,
			"forceEmail":false
		}
	}
	jQuery.post('ajaxCalls.php',data,function(response){
		$("#sendMessageBlock").modal("hide");
		
	},"json");
}

/*
	Method getShoppingList()
	Gets all items which have not yet been purchased by the user.
	
	@userid - the user id of the user for the requested shopping list.

*/

function getShoppingList(){
	data = {
		interact:'wishlist',
		action:'getShoppingList',
		args:{
			"userid":userId
		}
	}
	jQuery.post('ajaxCalls.php',data,function(response){	
		var shoppingListTable = $("#shoppingListTable").empty();
		var listTotal = 0;
		
		$(response).each(function(i,e){
			var itemDesc = $(document.createElement("td")).append(e.description);
			var forName = $(document.createElement("td")).append(e.fullname);
			var quantity = $(document.createElement("td")).append(e.quantity);
			var bestPrice = $(document.createElement("td")).append(storedData.currencySymbol+e.bestPrice).addClass("currency");
			
			var row = $(document.createElement("tr")).append(itemDesc,forName,quantity,bestPrice);
			listTotal += Number(e.bestPrice);
			
			
			shoppingListTable.append(row);
		});
		
		var cellName = $(document.createElement("td")).append("Total:").attr("colspan","3").addClass("bold");
		var cellTotal = $(document.createElement("td")).append(storedData.currencySymbol+listTotal.toPrecision(4)).addClass("currency bold");
		var totalRow = $(document.createElement("tr")).append(cellName,cellTotal);
		shoppingListTable.append(totalRow);
		
	},"json");
}

/*
	Method: manageRank
	
*/
function manageRank(){
	var rankId = $("input#rankId").val();
	var rankTitle = $("input#rankTitle").val();
	
	var data = {
		interact:'wishlist',
		action:'manageRank',
		args:{
			"rankid":rankId,
			"rankTitle":rankTitle
		}
	}

	jQuery.post('ajaxCalls.php',data,function(response){
		if(response){
			buildRankSelect();
		}
		$('#manageRankFormBlock').modal('hide');
	},"json");
}

/*
	Method: manageCategory
	
*/
function manageCategory(){
	var catId = $("input#categoryId").val();
	var catName = $("input#categoryName").val();
	
	data = {
		interact:'wishlist',
		action:'manageCategory',
		args:{
			"categoryid":catId,
			"category":catName
		}
	}
	jQuery.post('ajaxCalls.php',data,function(response){	
		$('#manageCategoryFormBlock').modal('hide');
			remoteBuildCategorySelect();
	},"json");	
}

/*
	Method: filterList
	Filters a list according to the field to search and the search term.
	field to search is not implemented yet.
*/
function filterList(wishlist, fieldToSearch, searchTerm){
	var returnList = [];
	

	for(i = 0; i< wishlist.list.length; i++){
		var term = new RegExp(searchTerm,"i");
		
		if(wishlist.list[i][fieldToSearch] != null && wishlist.list[i][fieldToSearch].match(term)){
			returnList.push(wishlist.list[i]);
		}
	}
	
return returnList;
}

/*
	Method: uploadImage
	
	checks form and executes form submission.
	
*/
function uploadImage(){
	if($("#uploadfile").val() != ""){
		$("imageUploadForm").submit();
	}else{
		$("#uploadAlertMessage").html("You must select a file for upload");
		$("#uploadAlert").removeClass("alert-success").addClass("alert-error").show();
		return false;
	}
}

/*
	Method: checkForUpdates
	
	Checks for updates and returns update information.
*/
function checkForUpdates(){
	
	var data = {
		'interact':'db',
		'action':'checkForUpdates'
	}

	$("#updateTextInfo").html("Checking for update...");

	jQuery.ajax({
		'data':data,
		'dataType':"json"		
	}).done(function(response,textStatus){
		if(response){
			$("#updateTextInfo").html(response.message);
			
			storedData.availableUpdate = response;
		}
	})
	
}

/*
	Method: getUpdate
	Makes a server call to download a previously found update and places it in the uploads directory.
*/

function getUpdate(){
	var data = {
		'interact':'db',
		'action':'downloadUpdateFile',
		'args':{
			"fileUri":storedData.availableUpdate.file,
			"fileName":storedData.availableUpdate.name
		}
	}

	$("#updateTextInfo").html("Checking for update..");

	jQuery.ajax({
		'data':data
	}).done(function(response,textStatus){
		if(response.updateDownloaded){
			storedData.updateLocation = response.file
			$("button#getUpdate").fadeOut();
			
		}
	})	
	
}


/*
	Method: applySystemUpdate
	
	Takes a file location, and calls the systemUpdate method serverside.
	
	@updateFileLocation - The downloaded file.
*/
function applySystemUpdate(){
	
	var data = {
		'interact':'db',
		'action':'systemUpdate',
		'args':{
			'updateFileLocation': storedData.filepath+storedData.updateLocation
		}
	}
	
	jQuery.ajax({
		'data':data,
		'dataType':"json"		
	}).done(function(response,textStatus){
		if(response){
			
		}
	})	
}

